package com.wannengbang.cloudleader.homepage;



import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BannerListBean;
import com.wannengbang.cloudleader.bean.ItemListDTO;
import com.wannengbang.cloudleader.bean.LuckDrawBean;
import com.wannengbang.cloudleader.bean.QueueRewardListBean;
import com.wannengbang.cloudleader.bean.ShopBindBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.dialog.AwardRuleDialog;
import com.wannengbang.cloudleader.dialog.LuckDrawDialog;
import com.wannengbang.cloudleader.dialog.MerchantRewardDialog;
import com.wannengbang.cloudleader.homepage.adapter.NoticeAdapter;
import com.wannengbang.cloudleader.homepage.adapter.NoticeDynamicListAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.mine.adapter.AddressListAdapter;
import com.wannengbang.cloudleader.mine.adapter.RedEnvelopeLatestAdapter;
import com.wannengbang.cloudleader.mine.model.MineModelImpl;
import com.wannengbang.cloudleader.shop.BindingMachinesActivity;
import com.wannengbang.cloudleader.shop.model.ShopModelImpl;
import com.wannengbang.cloudleader.utils.GlideUtils;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.utils.SPManager;
import com.wannengbang.cloudleader.utils.ScreenUtils;
import com.wannengbang.cloudleader.utilsview.ListViewForScrollView;
import com.wannengbang.cloudleader.widget.AutoScrollRecyclerView;
import com.wannengbang.cloudleader.widget.CommonNoTitleDialog;
import com.wannengbang.cloudleader.widget.RoundImageView;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DrawRedEnvelopesActivity extends BaseActivity {

    @BindView(R.id.iv_cj)
    ImageView iv_cj;

    @BindView(R.id.iv_cj_1)
    ImageView iv_cj_1;
    @BindView(R.id.iv_cj_2)
    ImageView iv_cj_2;
    @BindView(R.id.iv_cj_3)
    ImageView iv_cj_3;
    @BindView(R.id.iv_cj_4)
    ImageView iv_cj_4;
    @BindView(R.id.iv_cj_5)
    ImageView iv_cj_5;
    @BindView(R.id.iv_cj_6)
    ImageView iv_cj_6;
    @BindView(R.id.iv_cj_7)
    ImageView iv_cj_7;
    @BindView(R.id.iv_cj_8)
    ImageView iv_cj_8;

    @BindView(R.id.iv_zhongj_1)
    ImageView iv_zhongj_1;
    @BindView(R.id.iv_zhongj_2)
    ImageView iv_zhongj_2;
    @BindView(R.id.iv_zhongj_3)
    ImageView iv_zhongj_3;
    @BindView(R.id.iv_zhongj_4)
    ImageView iv_zhongj_4;
    @BindView(R.id.iv_zhongj_5)
    ImageView iv_zhongj_5;
    @BindView(R.id.iv_zhongj_6)
    ImageView iv_zhongj_6;
    @BindView(R.id.iv_zhongj_7)
    ImageView iv_zhongj_7;
    @BindView(R.id.iv_zhongj_8)
    ImageView iv_zhongj_8;

    @BindView(R.id.tv_hb_total_count)
    TextView tv_hb_total_count;
    @BindView(R.id.tv_hb_have_count)
    TextView tv_hb_have_count;
    @BindView(R.id.tv_hb_device_no_money)
    TextView tv_hb_device_no_money;
    @BindView(R.id.tv_hb_total_money)
    TextView tv_hb_total_money;
    @BindView(R.id.recyclerView_dynamic)
    AutoScrollRecyclerView recyclerViewDynamic;
    @BindView(R.id.tv_look_details)
    TextView tv_look_details;

    private CommonNoTitleDialog.Builder dialogBuild;

    private int count = 80;
    private int number = 0;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100) {
                if (count > 0) {
                    count--;
                    number++;
                    int l = count % 10;


                    if (number > 70 && number % 10 == num) {
                        number = 0;
                        count =80;
                        qq = false;
                        LuckDrawDialog dialog = LuckDrawDialog.newInstance(money);
                        dialog.show(getSupportFragmentManager(), "dialog");
                        return;
                    }
                    handler.sendEmptyMessageDelayed(100, 100);


                    Log.e("倒计时yu", l + "秒");
                    Log.e("number", number % 10 + "秒");

                    if (0 == l || 9 == l) {
                        iv_zhongj_1.setVisibility(View.VISIBLE);
                        iv_zhongj_8.setVisibility(View.GONE);

                    } else if (8 == l || 7 == l) {
                        iv_zhongj_2.setVisibility(View.VISIBLE);
                        iv_zhongj_1.setVisibility(View.GONE);

                    } else if (6 == l) {
                        iv_zhongj_3.setVisibility(View.VISIBLE);
                        iv_zhongj_2.setVisibility(View.GONE);

                    } else if (5 == l) {
                        iv_zhongj_4.setVisibility(View.VISIBLE);
                        iv_zhongj_3.setVisibility(View.GONE);

                    } else if (4 == l) {
                        iv_zhongj_5.setVisibility(View.VISIBLE);
                        iv_zhongj_4.setVisibility(View.GONE);

                    } else if (3 == l) {
                        iv_zhongj_6.setVisibility(View.VISIBLE);
                        iv_zhongj_5.setVisibility(View.GONE);

                    } else if (2 == l) {
                        iv_zhongj_7.setVisibility(View.VISIBLE);
                        iv_zhongj_6.setVisibility(View.GONE);
                    } else if (1 == l) {
                        iv_zhongj_8.setVisibility(View.VISIBLE);
                        iv_zhongj_7.setVisibility(View.GONE);
                    }

                } else {
                    qq = false;
                    handler.removeMessages(100);
                    count = 80;
                    LuckDrawDialog dialog = LuckDrawDialog.newInstance(money);
                    dialog.show(getSupportFragmentManager(), "dialog");
                }
            }
        }
    };
    private int num;
    private HomePageModelImpl homePageModel;
    private String money;
    private List<ItemListDTO.DataBean.itemList> lists;
    private NoticeAdapter noticeDynamicListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_red_envelopes);
        ButterKnife.bind(this);
        homePageModel = new HomePageModelImpl();
        initView();
        initData();
        setEvent();

    }

    private void initView() {

        lists = new ArrayList<>();

        noticeDynamicListAdapter = new NoticeAdapter(lists);
        recyclerViewDynamic.setLayoutManager(new WrapContentLinearLayoutManager(this));
        recyclerViewDynamic.setAdapter(noticeDynamicListAdapter);

    }


    private void initData() {
        new MineModelImpl().requestMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {

                String s = new Gson().toJson(bean);

                Log.e("ddddd",s);

                int hb_can_count = bean.getData().getHb_can_count();
                int hb_have_count = bean.getData().getHb_have_count();
                float hb_device_no_money = bean.getData().getHb_device_no_money();
                float hb_total_money = bean.getData().getHb_total_money();

                float ney = 10000 - hb_device_no_money / 100;

                tv_hb_total_count.setText("红包可抽奖 "+hb_can_count + "次");
                tv_hb_have_count.setText("红包已抽奖 "+hb_have_count + "次");
                tv_hb_device_no_money.setText("下次抽奖剩余金额 "+ ney+ "元");
                tv_hb_total_money.setText("已抽取红包总金额  "+hb_total_money/100 + "元");

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });


        homePageModel.requestAgentMoneyLogHbLotteryLog(new DataCallBack<ItemListDTO>() {
            @Override
            public void onSuccessful(ItemListDTO bean) {
                lists.clear();
                lists.addAll(bean.getData().getLists());
                noticeDynamicListAdapter.notifyDataSetChanged();


            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });

        new ShopModelImpl().requestIntegralBindIndex(new DataCallBack<ShopBindBean>() {
            @Override
            public void onSuccessful(ShopBindBean bean) {

                if (bean.getData() == null || bean.getData().size() == 0) {


                    showJjDialog();

                }else {
                    List<ShopBindBean.DataBean> data = bean.getData();
                    boolean isShow = false;
                    for (int i = 0; i < data.size(); i++) {
                        int type = data.get(i).getType();
                        if (type==5){
                            isShow = true;
                        }
                        if (!isShow){
                            showJjDialog();
                        }
                    }
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
    @Override
    protected void onStop() {
        super.onStop();
        recyclerViewDynamic.stop();
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onStart() {
        super.onStart();
        //流式滚动效果
        recyclerViewDynamic.start();
    }

    private void showJjDialog() {

        dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
        dialogBuild.setMessage("请先绑定红包机具，绑定后刷卡奖励");
        dialogBuild.setPositiveVersionButton("去绑定", v -> {
            dialogBuild.dismiss();
            dialogBuild = null;
            showActivity(BindingMachinesActivity.class);
        });
        dialogBuild.setNegativeButton("取消", v -> {
            dialogBuild.dismiss();
            dialogBuild = null;
        });
        dialogBuild.setCancelable(false);
        dialogBuild.show();

    }


    boolean qq ;
    private void setEvent() {
        iv_cj.setOnClickListener(v -> {
            getData();


        });
        tv_look_details.setOnClickListener(v -> {



            MerchantRewardDialog dialog = MerchantRewardDialog.newInstance();
            dialog.show(getSupportFragmentManager(), "dialog");

        });

    }

    /**
     * 生成一个0 到 count 之间的随机数
     *
     * @param endNum
     * @return
     */
    public static int getNum(int endNum) {
        if (endNum > 0) {
            Random random = new Random();
            return random.nextInt(endNum);
        }
        return 0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(100);
    }

    public void getData() {
        homePageModel.requestRedPacketRun(new DataCallBack<LuckDrawBean>() {
            @Override
            public void onSuccessful(LuckDrawBean bean) {
                money = bean.getData().getMoney();
                if (!qq){
                    qq = true;
                    num = getNum(8);
                    Log.e("随机数", num + "");
                    handler.sendEmptyMessageDelayed(100, 100);

                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });

    }
}